<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <!--<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />-->
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/bootstrap-3.3.4.css">
<link href="dist/summernote.css" rel="stylesheet"/>
        <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
        <script src="js/jquery.min.js" type="text/javascript"></script>
		<script src="js/jquery.cookie.js" type="text/javascript"></script>
		<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script src="http://www.jq22.com/jquery/bootstrap-3.3.4.js"></script>
<script src="dist/summernote.js"></script>
<script src="dist/lang/summernote-zh-CN.js"></script> 
<style>
	.m{ width: 800px; margin-left:100px; margin-right: auto; }
</style>
        <script type="text/javascript">
        	$(function(){
 $('.summernote').summernote({
        height: 200,
        tabsize: 2,
        lang: 'zh-CN'
    });
});
        	function saveArticle(){
			
			var title=$("#title").val();
			var summary=$("#summary").val();
			var keyWord=$("#keyWord").val();
			var author=$("#author").val();
			var source=$("#source").val();
			var imageUrl=$("#newfile").val();
			var token2=$.cookie("token2");
			var content=$('.summernote').summernote('code');
			var sort=$("#sort").val();
			$.ajax({
						type: 'POST',
						dataType: "json",
						url: 'http://localhost:9095/admin/addarticle/save',
						data: {
							"title": title,
							"summary": summary,
							"author": author,
							"source": source,
							"imageUrl": imageUrl,
							"content": content,
							"sort": sort,
							"keyWord": keyWord,
						},
                        headers:{
							"token2":token2
						},
						success: function(msg) {
							if(msg.data>0) {
								alert("新增成功")
								location.reload();
							}

						}

					});
		}
        	function getFileUrl(sourceId) {
                var url;
                if (navigator.userAgent.indexOf("MSIE")>=1) { // IE
                    url = document.getElementById(sourceId).value;
                } else if(navigator.userAgent.indexOf("Firefox")>0) { // Firefox
                    url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
                } else if(navigator.userAgent.indexOf("Chrome")>0) { // Chrome
                    url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
                }
                return url;
            }
            function preImg(sourceId, targetId) { 
                var url = getFileUrl(sourceId); 
                var imgPre = document.getElementById(targetId); 
                imgPre.src = url; 
            }
            
            
            
        	
        </script>
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                  <div class="layui-form-item">
                      <label for="username" class="layui-form-label">
                          <span class="x-red">*</span>标题
                          
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" id="title" name="username" required="" lay-verify="required"
                          autocomplete="off" class="layui-input">
                      </div>
                        <div class="layui-form-item layui-form-text">
            <label for="desc" class="layui-form-label">摘要</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" id="summary" name="desc" class="layui-textarea"></textarea>
            </div>
            </div>
                  
                  <div class="layui-form-item">
                      <label for="phone" class="layui-form-label">
                          <span class="x-red">*</span>文章关键字
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" id="keyWord" name="phone" required="" lay-verify="phone"
                          autocomplete="off" class="layui-input">
                      </div>
                  </div>
                  
                  <div class="layui-form-item">
                      <label for="L_email" class="layui-form-label">
                          <span class="x-red">*</span>作者
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" id="author" name="email" required="" lay-verify="email"
                          autocomplete="off" class="layui-input">
                      </div>
                  </div>
                 
                 <div class="layui-form-item">
                      <label for="L_email" class="layui-form-label">
                          <span class="x-red">*</span>来源
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" id="source" name="email" required="" lay-verify="email"
                          autocomplete="off" class="layui-input">
                      </div>
                  </div>
                  
                  <div class="layui-form-item">
                      <label for="L_email" class="layui-form-label">
                          <span class="x-red">*</span>封面图片
                      </label>
                     <div class="layui-upload upload">
            <button type="button" class="layui-form-label" id="upload">上传图片</button>
　　　　　　　<input  class="layui-upload-file" type="file" accept="" name="file">
          <input type="text" id="newfile" hidden="hidden" />
             <div class="layui-upload-list">
                   <img class="layui-upload-img" id="demo1">
                   <p id="demoText"></p>
             </div>
</div>
                  </div>
                  
                  </div>
                        <div class="layui-form-item">
                      <label for="L_email" class="layui-form-label">
                          <span class="x-red">*</span>类容
                      </label>
                      <div class="m">		
		<div class="summernote"></div>
	</div>
                  </div>
                  
            
            <div class="layui-form-item">
                      <label for="L_email" class="layui-form-label">
                          <span class="x-red">*</span>排序值
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" id="sort" name="email" required="" lay-verify="email"
                          autocomplete="off" class="layui-input">
                      </div>
                  </div>
                  
                  <div class="layui-form-item">
                      <label for="L_repass" class="layui-form-label">
                      </label>
                      <button  class="layui-btn" lay-filter="add" type="button" onclick="saveArticle()">
                              增加
                      </button>
                  </div>
              </form>
            </div>
        </div>
        <script>
        	
  layui.use(['laypage', 'layer', 'upload'], function () {
        var laypage = layui.laypage  //设置配置环境
            , layer = layui.layer
            , upload = layui.upload

     
        //上传图片
        var uploadInst = upload.render({
            elem: '#upload'
            , url: 'http://localhost:9095/admin/addarticle/getfile' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
               $("#newfile").val(res.name);
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
 }); 
        	
        	layui.use(['form', 'layer'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;

                //自定义验证规则
                form.verify({
                    nikename: function(value) {
                        if (value.length < 5) {
                            return '昵称至少得5个字符啊';
                        }
                    },
                    pass: [/(.+){6,12}$/, '密码必须6到12位'],
                    repass: function(value) {
                        if ($('#L_pass').val() != $('#L_repass').val()) {
                            return '两次密码不一致';
                        }
                    }
                });

                //监听提交
                form.on('submit(add)',
                function(data) {
                    console.log(data);
                    //发异步，把数据提交给php
                    layer.alert("增加成功", {
                        icon: 6
                    },
                    function() {
                        //关闭当前frame
                        xadmin.close();

                        // 可以对父窗口进行刷新 
                        xadmin.father_reload();
                    });
                    return false;
                });

            });
        </script>
        
    </body>

</html>
